package com.seecen.webproject.dao;

import com.seecen.webproject.bo.EmpBO;
import com.seecen.webproject.bo.Page;
import com.seecen.webproject.entity.Emp;
import com.seecen.webproject.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class EmpDao extends BaseDao {

    public int queryTotal(EmpBO queryBO) {
        List param = new ArrayList();
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM EMP WHERE 1=1 ");
        if (queryBO.getEmp() != null && !MyUtils.isEmpty(queryBO.getEmp().getEname())) {
            sql.append(" AND ENAME LIKE ? ");
            param.add("%" + queryBO.getEmp().getEname() + "%");
        }
        if (!MyUtils.isEmpty(queryBO.getQueryDate1())) {
            sql.append(" AND HIREDATE >= ? ");
            param.add(queryBO.getQueryDate1());
        }
        if (!MyUtils.isEmpty(queryBO.getQueryDate2())) {
            sql.append(" AND HIREDATE <= ? ");
            param.add(queryBO.getQueryDate2());
        }
        Object[] array = param.toArray();
        return queryCount(sql.toString(), array);
    }

    public List<Emp> queryPage(Page<List<Emp>, EmpBO> page) {
        List param = new ArrayList();
        StringBuilder sql = new StringBuilder("SELECT * FROM EMP WHERE 1=1 ");
        if (page.getQueryBO().getEmp() != null && !MyUtils.isEmpty(page.getQueryBO().getEmp().getEname())) {
            sql.append(" AND ENAME LIKE ? ");
            param.add("%" + page.getQueryBO().getEmp().getEname() + "%");
        }
        if (!MyUtils.isEmpty(page.getQueryBO().getQueryDate1())) {
            sql.append(" AND HIREDATE >= ? ");
            param.add(page.getQueryBO().getQueryDate1());
        }
        if (!MyUtils.isEmpty(page.getQueryBO().getQueryDate2())) {
            sql.append(" AND HIREDATE <= ? ");
            param.add(page.getQueryBO().getQueryDate2());
        }
        sql.append("LIMIT ");
        sql.append(page.getStartLimit());
        sql.append(",");
        sql.append(page.getPageCount());
        Object[] array = param.toArray();
        return query(Emp.class, sql.toString(), array);
    }
}
